#include <rtdef.h>
#include <rtthread.h>
#include "RTC_8025T.h"
#include "MQ.h"

struct rt_i2c_bus_device *RTC_8025Tdev;
rt_sem_t RTCsem;
    
_mRTC_U now;
TRealTimer RealTimer;

static void timer8025TOut(void * parameter)
{
    rt_sem_release(RTCsem);
}

static void RTC_8025T_timer_init()
{
    rt_timer_t  timerPower;
    timerPower = rt_timer_create( "timer8025T", timer8025TOut, RT_NULL, 500, RT_TIMER_FLAG_PERIODIC);
    rt_timer_start(timerPower);
}

void RTC_8025T_thread_entry(void *parameter)
{

    RTCsem = rt_sem_create("RTC8025T_sync", 0, RT_IPC_FLAG_FIFO);   
    RTC_8025T_timer_init();
    
    RTC_8025Tdev = (struct rt_i2c_bus_device *)rt_device_find("i2c1_8025T");
    if (RTC_8025Tdev)
    {
        RTC_8025T_init();
        RTC_8025T_Read(RTC_8025Tdev,now.All,7);
        rt_kprintf("successful: i2c1_8025T Device successful \n");
    }
    
    while(1)
    {
        rt_sem_take(RTCsem, RT_WAITING_FOREVER);
        if (RTC_8025Tdev)
        {
            RTC_8025T_Read(RTC_8025Tdev,now.All,7);
            lib_MultipleBBCDToBin(7,now.All,now.All);
            RealTimer.wYear = (now.Detail.YY+2000);
        
            RealTimer.Mon = now.Detail.MM;
            RealTimer.Day = now.Detail.DD;
            RealTimer.Hour = now.Detail.HH;
            RealTimer.Min = now.Detail.NN;
            RealTimer.Sec = now.Detail.SS;
        }
    }
}